package com.usfuture.ins.proxy
{
	import com.usfuture.ISocket;
	import com.usfuture.IntelAddr;
	import com.usfuture.event.DataEvent;
	import com.usfuture.ins.entity.Ins;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;

	public class InsProxy extends EventDispatcher implements ISocket
	{
		public static const NAME:String="insProxy";

		/**
		 * 通知加载即时数据
		 * @default
		 */
		public static const INS_DATA_LOAD:String="insDataLoad";
		/**
		 * 加载即时数据完成
		 * @default
		 */
		public static const INS_DATA_LOAD_COMPLETE:String="insDataLoadComplete";

		/**
		 * SOCKET ERROR
		 * @default
		 */
		public static const INS_SOCKET_ERROR:String="insSocketError";

		private var _socket:Socket;

		private var _dataStr:String='';
		
		private var _data:Array;

		public function connection():void
		{
			_socket=new Socket();
			_socket.addEventListener(Event.CLOSE, closehandler);
			_socket.addEventListener(Event.CONNECT, connectHandle);
			_socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandle);
			_socket.addEventListener(ProgressEvent.SOCKET_DATA, recieveHandle);
			_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandle);
			_socket.connect(IntelAddr.IP, IntelAddr.LOOKUP_PORT);
			
		}

		public function recieveHandle(event:ProgressEvent):void
		{
			if(_socket&&_socket.connected)
				_dataStr+=_socket.readUTFBytes(_socket.bytesAvailable);
			if (_dataStr.lastIndexOf('end') != -1)
			{
				var arr:Array=_dataStr.split("\n");
				for (var i:int=0; i < arr.length; i++)
				{
					var realData:Array=String(arr[i]).split(',');
					if (realData.length < 2)
						continue;
					var ins:Ins=new Ins();
//					ins.timestamp=new Date(realData[0]);
//					ins.newPrice=realData[2];
//					ins.allVolume=realData[3];

					ins.timestamp=new Date(Number(realData[0]));
					ins.newPrice=realData[4];
					ins.allVolume=realData[5];
					
					
					_data.push(ins);
				}
				close();
				_dataStr='';
				this.dispatchEvent(new DataEvent(DataEvent.DATA_LOAD_COPMLEATE,_data));
			}
		}

		public function sendHandle(data:Object):void
		{
			var _lineType:String=data['lineType'];
			var maxTime:String=data['maxTime'];
			var redirect:String=data['redirect'];
			if(_socket){
				_socket.writeUTFBytes("line," + _lineType + ",A," + maxTime + ","+redirect+",end\r\n");
				_socket.flush();
			}
		}

		public function close():void
		{
			_dataStr='';
			if (_socket){
				_socket.removeEventListener(Event.CLOSE, closehandler);
				_socket.removeEventListener(Event.CONNECT, connectHandle);
				_socket.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandle);
				_socket.removeEventListener(ProgressEvent.SOCKET_DATA, recieveHandle);
				_socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandle);
				_socket.close();
			}
			_socket=null;
		}
		
		
		public function connectHandle(evt:Event):void
		{
			trace("连接建立");
			_data = new Array;
		}

		public function closehandler(evt:Event):void
		{
			trace("连接关闭");
			close();
		}


		public function ioErrorHandle(evt:IOErrorEvent):void
		{
			trace("io异常");
			close();
		}

		public function securityHandle(evt:SecurityErrorEvent):void
		{
			trace("安全异常");
			close();
		}
	}
}